package com.yanqu.road.server.cmd.monopolymarket.resgrab;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.monopolymarket.resgrab.IMonopolyMarketObjectInfo;
import com.yanqu.road.entity.monopolymarket.MonopolyMarket;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketBeautyInfo;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketPatronsInfo;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrab;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketResGrabPointInfo;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketFortressMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Set;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/6 20:14
 */
@Cmd(code = Protocol.C_CROSS_MONOPOLY_MARKET_SYN_DISPATCH_INFO, desc = "同步有变化的派遣对象信息")
public class CrossMonopolyMarketResGrabSynDispatchCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketSynDispatchToCrossReqMsg reqMsg = MonopolyMarketProto.MonopolyMarketSynDispatchToCrossReqMsg.parseFrom(packet.getBytes());
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,false);
        if(monopolyMarket == null){
            return;
        }
        synchronized (monopolyMarket) {
            if (reqMsg.getResGrabPatronsListCount() > 0) {
                MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(eMonopolyMarketResType.RES_KING.getValue());
                if (resGrab == null) {
                    return;
                }
                MonopolyMarketMgr.synPatrons(resGrab, packet.getPlayerId(), reqMsg.getResGrabPatronsListList());
            }
            if (reqMsg.getResGrabBeautyListCount() > 0) {
                MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(eMonopolyMarketResType.RES_QUEEN.getValue());
                if (resGrab == null) {
                    return;
                }
                MonopolyMarketMgr.synBeauty(resGrab, packet.getPlayerId(), reqMsg.getResGrabBeautyListList());
            }
            if (reqMsg.getFortressPatronsListCount() > 0) {
                MonopolyMarketFortressMgr.synFortressPatrons(monopolyMarket, packet.getPlayerId(), reqMsg.getFortressPatronsListList());
            }

        }
    }

    @Override
    public void sendData() {

    }

    private static void synPatrons(MonopolyMarketResGrab resGrab, long userId, List<MonopolyMarketProto.MonopolyMarketPatronsEntity> list){
        Set<MonopolyMarketResGrabPointInfo> pointInfos =  resGrab.getUserPointSet(userId,false);
        if(pointInfos != null) {
            for (MonopolyMarketProto.MonopolyMarketPatronsEntity item : list) {
                for (MonopolyMarketResGrabPointInfo pointInfo : pointInfos) {
                    if(pointInfo.baseInfo.getUserId() != userId){
                        continue;
                    }
                    IMonopolyMarketObjectInfo objectInfo = pointInfo.objectInfoMap.get(item.getPatronsId());
                    if (objectInfo != null) {
                        MonopolyMarketPatronsInfo patronsInfo = (MonopolyMarketPatronsInfo) objectInfo;
                        patronsInfo.setStageLv(item.getStageLv());
                        patronsInfo.setLevel(item.getLevel());
                        patronsInfo.setSkinId(item.getSkinId());
                        patronsInfo.setEarn(item.getEarn());
                        patronsInfo.setAddPlus(item.getAddPlus());
                        pointInfo.baseInfo.setUpdateOption();
                        resGrab.updateAndAddPointInfoSet.add(pointInfo);
                        break;
                    }
                }
            }
        }
    }

    private static void synBeauty(MonopolyMarketResGrab resGrab, long userId, List<MonopolyMarketProto.MonopolyMarketBeautyEntity> list){
        Set<MonopolyMarketResGrabPointInfo> pointInfos =  resGrab.getUserPointSet(userId,false);
        if(pointInfos != null) {
            for (MonopolyMarketProto.MonopolyMarketBeautyEntity item : list){
                for (MonopolyMarketResGrabPointInfo pointInfo : pointInfos){
                    if(pointInfo.baseInfo.getUserId() != userId){
                        continue;
                    }
                    IMonopolyMarketObjectInfo objectInfo = pointInfo.objectInfoMap.get(item.getId());
                    if(objectInfo != null){
                        MonopolyMarketBeautyInfo patronsInfo = (MonopolyMarketBeautyInfo)objectInfo;

                        patronsInfo.setId(item.getId());
                        patronsInfo.setValue(item.getValue());
                        patronsInfo.setSkinId(item.getSkinId());
                        patronsInfo.setAddPlus(item.getAddPlus());
                        pointInfo.baseInfo.setUpdateOption();
                        resGrab.updateAndAddPointInfoSet.add(pointInfo);
                        break;
                    }
                }
            }
        }
    }

}
